import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter';
import Toast from 'tdesign-miniprogram/toast/index';
import ActionSheet, { ActionSheetTheme } from 'tdesign-miniprogram/action-sheet/index';
import i18nInstance from "../../miniprogram_npm/miniprogram-i18n-plus/index"
let i18nl = i18nInstance.getLanguage();

const menuData = [
  [
    {
      title: i18nl.hotline,
      tit: '',
      url: '',
      type: 'phone',
    },
    {
      title: '语言/Language',
      tit: '',
      url: '',
      type: 'language',
    },
  ],
];


const getDefaultData = () => ({
  showMakePhone: false,
  userInfo: {
    avatarUrl: '',
    nickName: '正在登录...',
    phoneNumber: '',
  },
  menuData,
  customerServiceInfo: {},
  currAuthStep: 2,
  showKefu: true,
  versionNo: '',
});

Page({
  data: getDefaultData(),

  onLoad() {
    this.getVersionInfo();
  },

  onShow() {
    this.getTabBar().init();
    this.init();
  },
  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.fetUseriInfoHandle();
  },

  fetUseriInfoHandle() {
    fetchUserCenter().then(
      ({
        userInfo,
        countsData,
        customerServiceInfo,
      }) => {
        // eslint-disable-next-line no-unused-expressions
        menuData?.[0].forEach((v) => {
          countsData.forEach((counts) => {
            if (counts.type === v.type) {
              // eslint-disable-next-line no-param-reassign
              v.tit = counts.num;
            }
          });
        });
        this.setData({
          userInfo,
          menuData,
          customerServiceInfo,
          currAuthStep: 2,
        });
        wx.stopPullDownRefresh();
      },
    );
  },

  onClickCell({ currentTarget }) {
    const { type } = currentTarget.dataset;

    switch (type) {
      case 'language': {
        this.handleAction();
        break;
      };
      case 'phone': {
        this.handleAction_hotline();
        break;
      }
    }
  },

  gotoUserEditPage() {
    const { currAuthStep } = this.data;
    if (currAuthStep === 2) {
      wx.navigateTo({ url: '/pages/usercenter/person-info/index' });
    } else {
      this.fetUseriInfoHandle();
    }
  },

  handleAction() {
    ActionSheet.show({
      theme: ActionSheetTheme.List,
      selector: '#t-action-sheet',
      context: this,
      items: [
        {
          label: '中文',
        },
        {
          label: 'English',
        },
        {
          label: 'français',
        },
        {
          label: '日本語',
        },
      ],
    });
  },
  handleAction_hotline() {
    ActionSheet.show({
      theme: ActionSheetTheme.List,
      selector: '#t-action-sheet-hotline',
      context: this,
      items: [
        {
          label: '10086',
        },
        {
          label: '10000',
        }
      ],
    });
  },
  handleSelected(e) {
    console.log(e.detail);
    switch (e.detail.index) {
      case 0:
        wx.setStorageSync('lang', 'zh_CN')
        wx.reLaunch({
          url: '/pages/home/home',
        })
        break;
      case 1:
        wx.setStorageSync('lang', 'en_US')
        wx.reLaunch({
          url: '/pages/home/home',
        })
        break;
      case 2:
        wx.setStorageSync('lang', 'French')
        wx.reLaunch({
          url: '/pages/home/home',
        })
        break;
      case 3:
        wx.setStorageSync('lang', "Japan"),
          wx.reLaunch({
            url: '/pages/home/home',
          })
    }
  },
  handleSelected_hotline(e) {
    console.log(e.detail);
    switch (e.detail.index) {
      case 0:
        wx.makePhoneCall({
          phoneNumber: '10086',
        })
        break;
      case 1:
        wx.makePhoneCall({
          phoneNumber: '10000',
        })
        break;
    }
  },
});
